package com.lagou.dao.impl;

import com.lagou.dao.CourseDao;
import com.lagou.pojo.Course;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.management.Query;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CourseDaoImpl implements CourseDao {
    @Override
    public List<Course> findCourseList() {


        List<Course> result = null;
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "select id, course_name, price, sort_num, status from course where is_del = 0";

            result = qr.query(sql, new BeanListHandler<Course>(Course.class));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
        }

        return result;
    }

    @Override
    public List<Course> findByCourseNameAndStatus(String courseName, String status) {

        List<Course> result = null;
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            StringBuilder sb = new StringBuilder("select id, course_name, price, sort_num, status from course where 1=1 and is_del = ?");

            List<Object> params = new ArrayList<>();
            params.add(0);

            if (null != courseName && courseName.length() > 0) {
                sb.append(" AND course_name like ?");
                courseName = "%" + courseName + "%";
                params.add(courseName);
            }

            if (null != status && status.length() > 0) {
                sb.append(" AND status = ?");
                params.add(Integer.parseInt(status));
            }

            result = qr.query(sb.toString(), new BeanListHandler<Course>(Course.class), params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;

    }

    /**
     * 保存课程的营销信息
     * @param course
     * @return
     */
    @Override
    public int saveCourseSalesInfo(Course course) {

        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "INSERT INTO course (" +
                    "  course_name," +
                    "  brief," +
                    "  teacher_name," +
                    "  teacher_info," +
                    "  preview_first_field," +
                    "  preview_second_field," +
                    "  discounts," +
                    "  price," +
                    "  price_tag," +
                    "  share_image_title," +
                    "  share_title," +
                    "  share_description," +
                    "  course_description," +
                    "  course_img_url," +
                    "  STATUS," +
                    "  create_time," +
                    "  update_time" +
                    "  )VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            Object[] params = {
                    course.getCourse_name(),
                    course.getBrief(),
                    course.getTeacher_name(),
                    course.getTeacher_info(),
                    course.getPreview_first_field(),
                    course.getPreview_second_field(),
                    course.getDiscounts(),
                    course.getPrice(),
                    course.getPrice_tag(),
                    course.getShare_image_title(),
                    course.getShare_title(),
                    course.getShare_description(),
                    course.getCourse_description(),
                    course.getCourse_img_url(),
                    course.getStatus(),
                    course.getCreate_time(),
                    course.getUpdate_time()
            };
            return qr.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public Course findCourseById(int id) {

        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "SELECT id, course_name, brief, teacher_name, teacher_info, preview_first_field, preview_second_field, discounts, price, price_tag, course_img_url,\n" +
                    "share_title, share_description, course_description, STATUS FROM course WHERE id = ?";
            Course result = qr.query(sql, new BeanHandler<Course>(Course.class), id);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int updateCourseSaleInfo(Course course) {

        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "UPDATE course SET course_name = ?, brief = ?, teacher_name = ?, teacher_info = ?, preview_first_field = ?," +
                    "preview_second_field = ?, discounts = ?, price = ?, price_tag = ?, share_image_title=?, share_title = ?," +
                    "share_description = ?, course_description = ?, course_img_url = ?, update_time=? WHERE id = ?";

            Object[] params = {
                    course.getCourse_name(),
                    course.getBrief(),
                    course.getTeacher_name(),
                    course.getTeacher_info(),
                    course.getPreview_first_field(),
                    course.getPreview_second_field(),
                    course.getDiscounts(),
                    course.getPrice(),
                    course.getPrice_tag(),
                    course.getShare_image_title(),
                    course.getShare_title(),
                    course.getShare_description(),
                    course.getCourse_description(),
                    course.getCourse_img_url(),
                    course.getUpdate_time(),
                    course.getId()
            };
            return qr.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateCourseStatus(Course course) {

        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "update course set status = ?, update_time = ? where id = ?";
            Object[] params = {course.getStatus(), course.getUpdate_time(), course.getId()};
            return qr.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }
}
